<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录查询</title>
    <script src="jquery-3.4.1.js"></script>
    <script src="ajax.js"></script>
    <script src="common.js"></script>
</head>
<body>
<div>
    <button><a href="debackAdd.html">新增</a></button>
    <button id="query">查询</button>
</div>
<table width="600" border="1" cellspacing="0">
    <thead>
    <th >Id</th>
    <th>名字</th>
    <th>密码</th>
    <th>操作</th>
    </thead>
    <tbody id="debackList">

    </tbody>
</table>
<div>
    <button id="pre">&lt;</button>
    <span id="pageBtn">

    </span>
    <button id="next">&gt;</button>
</div>
<script>
    let queryDom=$("#query");
    queryDom.addEventListener("click",function (e) {
        query();
    });
    let size=10;
    let page=1;
    let maxCount=0;
    $(function query() {
            function queryCallBack(data) {
                if(data.code=="200"){
                   let pageBtnDom=$("#pageBtn");
                   removeAllChild(pageBtnDom);
                   maxCount=data.data.count;
                    for(let i=1;i<=Math.ceil(data.data.count/size);i++)
                    {
                        let btn=$("body").append("button");
                        btn.innerText=i;
                        $(btn).click(function (e) {
                            page=e.target.innerText;
                            let formData=new FormData();
                            $(formData).append("size",size);
                            $(formData).append("page",page);
                            ajax("http://120.79.234.76:8091/login/list.php","POST",formData,queryCallBack);
                        });
                        $("#pageBtn").append(btn);
                    }
                   let debackList=$("#debackList");
                    removeAllChild(debackList);
                    for(let i=0;i<data.data.list.length;i++){
                        let deback=data.data.list[i];
                        let tr=$("body").append("tr");

                        let td=$("body").append("td");
                        $("td").addClass("id");
                        td.innerText=deback.id;
                        tr.appendChild(td);

                        td=$("body").append("td");
                        $("td").addClass("name");
                        td.innerText=deback.name;
                        tr.appendChild(td);

                        td=$("body").append("td");
                        $("td").addClass("password");
                        td.innerText=deback.password;
                        tr.appendChild(td);

                        td=$("body").append("td");
                        let btn=$("body").append("button");
                        btn.innerText="修改";
                        $(btn).click(function (e) {
                            let td=e.target.parentNode.parentNode;
                            let id=td.querySelector(".id").innerText;
                            location.href="/debackChange.html?id="+id;
                        });
                        td.appendChild(btn);
                        btn=$("body").append("button");
                        btn.innerText="删除";
                        $(btn).click(function (e) {
                            let td=e.target.parentNode.parentNode;
                            let id=td.querySelector(".id").innerText;
                            if(confirm("是否删除?")){
                                let formDataShit=new FormData();
                                formDataShit.append("id",id );
                                ajax("http://120.79.234.76:8091/login/delete.php","POST", formDataShit,function () {
                                    query();
                                });
                            }
                            query();
                        });
                        td.appendChild(btn);
                        tr.appendChild(td);

                        debackList.appendChild(tr);//插入一行
                    }
                }else {
                    alert(data.msg)
                }
            }
        let formData=new FormData();
        $(formData).append("size",size);
        $(formData).append("page",page);
        ajax("http://120.79.234.76:8091/login/list.php","POST",formData,queryCallBack);
    })
    query();
    $("#pre").click(function (e) {
        page--;
        if(page<1){
            page=1;
            return;
        }
        query();
    })
    $("#next").click(function (e) {
        page++;
        if (page > Math.ceil(maxCount / size)) {
            page = Math.ceil(maxCount / size);
            return;
        }
        query();
    })
</script>
</body>
</html>